package org.jeecg.modules.scm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.scm.common.util.StringConversion;
import org.jeecg.modules.scm.dto.PageSearchOfChainDto;
import org.jeecg.modules.scm.entity.ScmCusInfo;
import org.jeecg.modules.scm.entity.ScmItem;
import org.jeecg.modules.scm.entity.ScmSupplyChain;
import org.jeecg.modules.scm.mapper.ScmSupplyChainMapper;
import org.jeecg.modules.scm.service.IScmCusInfoService;
import org.jeecg.modules.scm.service.IScmItemService;
import org.jeecg.modules.scm.service.IScmSupplyChainService;
import org.jeecg.modules.scm.vo.CusChainInfoVo;
import org.jeecg.modules.scm.vo.SupplyChainVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: scm_supply_chain
 * @Author: jeecg-boot
 * @Date: 2021-10-28
 * @Version: V1.0
 */
@Service
public class ScmSupplyChainServiceImpl extends ServiceImpl<ScmSupplyChainMapper, ScmSupplyChain> implements IScmSupplyChainService {

    @Autowired
    private IScmCusInfoService cusInfoService;
    @Autowired
    private IScmItemService itemService;


    @Override
    public List<CusChainInfoVo> getCusChainInfo(Integer itemId) {
        return baseMapper.getCusChainInfo(itemId);
    }


    /**
     * 模糊查询
     * */
    @Override
    public List<ScmSupplyChain> fuzzyQueryByField(String fields,String type, String value) {
//        QueryWrapper<ScmSupplyChain> queryWrapper = new QueryWrapper<ScmSupplyChain>();
        String res= StringConversion.changeForFuzzy(value);
//        queryWrapper.select(type).like(type,res);
//        List<ScmSupplyChain> list = baseMapper.selectList(queryWrapper);
//        List<String> result = StringConversion.getObjectProperty(list,type);

        String selStr = StringUtils.isNotBlank(fields)?fields+type:type;
        List<ScmSupplyChain> supplyChains = query().select(selStr).like(res, value).list();


        return supplyChains;
    }

    /**
     * 插入时前端传输对象转化为供应链实体类
     * */
    @Override
    public ScmSupplyChain modifyDto2Enity(ScmSupplyChain scmSupplyChain) {
        //根据客户名称、证件类型、证件号获取客户号
        ScmCusInfo scmCusInfo = cusInfoService.lambdaQuery()
                .eq(ScmCusInfo::getCusNo, scmSupplyChain.getRelevantCusNo())
                .one();
        if(scmCusInfo==null){
            throw new JeecgBootException("上下游客户信息不存在");
        }
        //根据核心企业名称获取核心客户号
        boolean exists = cusInfoService.lambdaQuery()
                .eq(ScmCusInfo::getCusNo, scmSupplyChain.getCoreCusNo())
                .exists();
        if(!exists){
            throw new JeecgBootException("核心客户信息不存在");
        }
        //根据核心企业和项目名称获取项目编号
        boolean exists1 = itemService.lambdaQuery().eq(ScmItem::getItemId, scmSupplyChain.getItemId())
                .exists();
        if(!exists1){
            throw new JeecgBootException("项目信息不存在");
        }
        //设置状态,默认1
        scmSupplyChain.setState(0);
        //检查是否在相同供应链关系
        ScmSupplyChain supplyChain = lambdaQuery().eq(ScmSupplyChain::getCoreCusNo, scmSupplyChain.getCoreCusNo())
                .eq(ScmSupplyChain::getRelevantCusNo, scmSupplyChain.getRelevantCusNo())
                .eq(ScmSupplyChain::getItemId, scmSupplyChain.getItemId())
                .eq(ScmSupplyChain::getRelationInChain, scmSupplyChain.getRelationInChain())
                .one();
        if(supplyChain!=null){
            throw new JeecgBootException("供应链关联关系已存在");
        }
        //客户经理
        scmSupplyChain.setCusManager(scmCusInfo.getCusManage());
        //所属机构
        scmSupplyChain.setPlatformAffiliation(scmCusInfo.getPlatformAffiliation());

        return scmSupplyChain;
    }

    /**
     * 实体类转化为展示对象
     * */
    @Override
    public SupplyChainVo modifyEnity2Vo(ScmSupplyChain scmSupplyChain) {
        SupplyChainVo supplyChainVo = new SupplyChainVo();
        BeanUtils.copyProperties(scmSupplyChain,supplyChainVo);
        return supplyChainVo;
    }

    /**
     * 跨表联合查询
     * */
    @Override
    public List<SupplyChainVo> pageSearch(PageSearchOfChainDto pageSearchOfChainDto) {

        return baseMapper.pageSearch(pageSearchOfChainDto);
    }




}
